package cn.offcn.service.Impl;

import cn.offcn.entity.*;
import cn.offcn.mapper.EmpRoleMapper;
import cn.offcn.mapper.RoleMapper;
import cn.offcn.mapper.RoleSourcesMapper;
import cn.offcn.service.RoleService;
import cn.offcn.utils.OAResult;
import cn.offcn.utils.TableVo;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yuliantao
 * @create 2020 -09 -2020-09-18 19:48
 * @description 功能描述
 */
@Service
public class roleServiceImpl implements RoleService {

    @Autowired
      private   RoleSourcesMapper roleSourcesMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private EmpRoleMapper empRoleMapper;


    @Override
    public OAResult addRole(Role role, String sourcesIds) {

        int insert = roleMapper.insert(role);
        if (StringUtils.isNotBlank(sourcesIds)){
            String[] split = sourcesIds.split (",");
            int count=0;
            for (String id : split) {
                RoleSources roleSources = new RoleSources();
                roleSources.setResourcesFk(Integer.parseInt(id));
                roleSources.setRoleFk(role.getRoleid());
                roleSourcesMapper.insert(roleSources);
                count++;
            }
            if (insert==1&&count==split.length){
                return  OAResult.ok(200,"添加成功！");
            }
        }
        return OAResult.ok(400,"添加失败");
    }


    @Override
    public TableVo<Role> getAllRolers(int page, int limit) {

        PageHelper.startPage(page,limit);

        List<Role> rolesList = roleMapper.selectByExample(new RoleExample());
        PageInfo<Role> pageInfo = new PageInfo<>(rolesList);

        TableVo<Role> tableVo = new TableVo<>();
        tableVo.setCount(pageInfo.getTotal());
        tableVo.setData(pageInfo.getList());
        return tableVo;
    }

    @Override
    public OAResult deleteRoleByRoleid(int roleid) {

        RoleSourcesExample roleSourcesExample = new RoleSourcesExample();
        RoleSourcesExample.Criteria criteria = roleSourcesExample.createCriteria();
        criteria.andResourcesFkEqualTo(roleid);
        roleSourcesMapper.deleteByExample(roleSourcesExample);


        EmpRoleExample empRoleExample = new EmpRoleExample();
        EmpRoleExample.Criteria criteria1 = empRoleExample.createCriteria();
        criteria1.andEmpFkEqualTo(roleid);
        empRoleMapper.deleteByExample(empRoleExample);

        int i = roleMapper.deleteByPrimaryKey(roleid);

        if (i==1){
            return OAResult.ok(200,"删除成功") ;

        }
        return  OAResult.ok(400,"删除失败");

    }

    @Override
    public Map getRoleByRoleid(int roleid) {

        Role role = roleMapper.selectByPrimaryKey(roleid);

        RoleSourcesExample roleSourcesExample = new RoleSourcesExample();
        RoleSourcesExample.Criteria criteria = roleSourcesExample.createCriteria();
        criteria.andRoleFkEqualTo(roleid);

        List<RoleSources> roleSources = roleSourcesMapper.selectByExample(roleSourcesExample);

         List<Integer> sourcesIds = new ArrayList<>();

        for (RoleSources roleSource : roleSources) {
            sourcesIds.add(roleSource.getResourcesFk());
        }
        Map map = new HashMap ();

        map.put("role",role);
        map.put("sourcesIds",sourcesIds);
        return map;
    }

    @Override
    public OAResult updateRole(Role role, String sourcesIds) {

        int rows = roleMapper.updateByPrimaryKey(role);

        RoleSourcesExample roleSourcesExample = new RoleSourcesExample();
        RoleSourcesExample.Criteria criteria = roleSourcesExample.createCriteria();
        criteria.andResourcesFkEqualTo(role.getRoleid()) ;

        roleSourcesMapper.deleteByExample(roleSourcesExample);

        //重新在添加
        if(StringUtils.isNotBlank(sourcesIds)){
            String[] ids=sourcesIds.split(",");
            int count=0;
            for(String id : ids){
                RoleSources roleSources=new RoleSources();
                roleSources.setResourcesFk(Integer.parseInt(id));
                roleSources.setRoleFk(role.getRoleid());
                roleSourcesMapper.insert(roleSources);
                count++;
            }
            if(rows==1 && count==ids.length){
                return OAResult.ok(200,"角色修改成功");
            }
        }
        return OAResult.ok(400,"角色修改失败");


    }
}
